﻿namespace Pps.Gui.Lcd
{
    using System.Threading;

    public delegate void TouchEventHandler(Touch data);
    public delegate void BacklitEventHandler();

    public abstract class Display
    {
        public event TouchEventHandler TouchDown;
        public event TouchEventHandler TouchUp;
        public event TouchEventHandler TouchMove;

        public event BacklitEventHandler BacklightOff;
        public event BacklitEventHandler BacklightOn;

        private Timer backlightTimer;

        private int backlightPeriod;

        public Display()
        {
            this.backlightPeriod = Timeout.Infinite;

            this.backlightTimer = new Timer(new TimerCallback(Display.BacklightTimerInterrupt), this, Timeout.Infinite, this.backlightPeriod);
        }

        private static void BacklightTimerInterrupt(object display)
        {
            Display lcd = display as Display;

            if (lcd != null)
            {
                lcd.BacklightTurnOff();
            }
        }

        internal void Initialize()
        {
            this.ConfigureDisplay();
            this.ConfigureTouch();
        }

        protected virtual void ConfigureDisplay()
        {
        }

        protected virtual void ConfigureTouch()
        {
        }

        /// <summary>
        /// Sets the time after the display is turned off
        /// </summary>
        /// <param name="seconds">Time in milliseconds. To disable this function use Timeout.Infinite.</param>
        public void SetBacklightAutoTurnOff(int milliseconds)
        {
            if (milliseconds > 0)
            {
                this.backlightPeriod = milliseconds;

                if (this.backlightTimer != null)
                {
                    this.backlightTimer.Change(this.backlightPeriod, Timeout.Infinite);
                }
            }
        }

        protected void ResetBacklightTimer()
        {
            if (this.backlightPeriod > Timeout.Infinite)
            {
                this.backlightTimer.Change(this.backlightPeriod, Timeout.Infinite);

                this.BacklightTurnOn();
            }
        }

        public virtual void BacklightTurnOff()
        {
        }

        public virtual void BacklightTurnOn()
        {
        }

        protected void OnTouchDown(Touch data)
        {
            if (this.TouchDown != null)
            {
                this.TouchDown(data);
            }
        }

        protected void OnTouchUp(Touch data)
        {
            if (this.TouchUp != null)
            {
                this.TouchUp(data);
            }
        }

        protected void OnTouchMove(Touch data)
        {
            if (this.TouchMove != null)
            {
                this.TouchMove(data);
            }
        }

        protected void OnBacklightOn()
        {
            if (this.BacklightOn != null)
            {
                this.BacklightOn();
            }
        }

        protected void OnBacklightOff()
        {
            if (this.BacklightOff != null)
            {
                this.BacklightOff();
            }
        }
    }
}